package com.sz.springboot_jpa.service.impl;

import com.sz.springboot_jpa.entity.Article;
import com.sz.springboot_jpa.entity.Tag;
import com.sz.springboot_jpa.entity.User;
import com.sz.springboot_jpa.pojo.ResponseCode;
import com.sz.springboot_jpa.repository.ArticleRepository;
import com.sz.springboot_jpa.repository.TagRepository;
import com.sz.springboot_jpa.service.ArticleServer;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ArticleServerImpl implements ArticleServer {

    @Resource
    private ArticleRepository articleRepository;
    @Resource
    private TagRepository tagRepository;

    @Override
    public void post(Article article) {
        article.setCreated(LocalDateTime.now());
        //处理所有的tags
        Set<Tag> tagSet = processTags(article.getTags());
        article.setTags(tagSet);
        //处理 摘要
        processSummary(article);
        articleRepository.save(article);
    }



    @Override
    public Article findById(Long id) {
        return articleRepository.findById(id).get();
    }

    /**
     * 书文广场，显示所有文章书籍
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public ResponseCode find(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageSize,pageNum);
        List<Article> list = articleRepository.findAllBy();
//        while (iterable.iterator().hasNext()){
//            list.add(iterable.iterator().next());
//        }
        PageInfo<Article> PInfo = new PageInfo<>(list);
        ResponseCode code = new ResponseCode();
        code.setData(list);
        code.setCount(PInfo.getTotal());
        code.setCode(0);

        return code;
    }

    @Override
    public Optional<Article> showDetailById(Long id) {
        Optional<Article> article = articleRepository.findById(id);

        return article;
    }

    /**
     * 保存一个article对象
     * @param article
     * @return
     */
    @Override
    public Article update(Article article) {
        return articleRepository.save(article);
    }

    /**
     * 通过id删除指定的文章
     * @param id
     */
    @Override
    public void delById(Long id) {
        articleRepository.deleteById(id);
    }


    private void processSummary(Article article) {
        if(article.getContent().length() > 50) {
            article.setSummary(article.getContent().substring(0,50));
        } else {
            article.setSummary(article.getContent());
        }
    }

    private Set<Tag> processTags(Set<Tag> tags){
        Set<Tag> tagSet = new HashSet<>();
        for(Tag t:tags){
            Tag temp = tagRepository.findByName(t.getName());

            if(temp == null){
                tagRepository.save(t);
                tagSet.add(t);
            }else{
                tagSet.add(temp);
            }
        }
        return tagSet;
    }
}
